package com.example.springboot.controller;

import cn.hutool.core.util.RandomUtil;
import com.example.springboot.pojo.PageBean;
import com.example.springboot.pojo.Result;
import com.example.springboot.pojo.User;
import com.example.springboot.pojo.Worker;
import com.example.springboot.util.Encrypt;
import com.example.springboot.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.springboot.service.WorkerService;

import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/worker")
public class WorkerController {

    @Autowired
    private WorkerService workerService;

    //
    @GetMapping("/findByWorkerId")
    public Worker findByWorkerId(String workerId) {
        return workerService.findByWorkerId(workerId);
    }

//    @GetMapping("/workerInfo")
//    public Result<?> adminInfo(@RequestHeader(name = "Authorization") String token){
//        Map<String, Object> map = JwtUtil.parseToken(token);
//        return Result.success(map);
//    }

    @GetMapping("/getAllWorker")
    public Result<PageBean<Worker>> getAllWorker(Integer pageNum, Integer pageSize) {
        PageBean<Worker> pb = workerService.getAllWorker(pageNum, pageSize);
        return Result.success(pb);
    }

    @PostMapping("/addWorker")
    public Result<?> addWorker(String name, String id, String phoneNumber, String department,String email) {
        String workerId = "";
        while (true) {
            workerId = id.substring(id.length() - 5,id.length() - 1) + RandomUtil.randomNumbers(2);
            Worker w = findByWorkerId(workerId);
            if (w == null) {
                break;
            }
        }
        workerService.addWorker(workerId, name, id.substring(id.length() - 6), id, phoneNumber, department,email);
        return Result.success();
    }

    @PutMapping("/updateWorker")
    public Result<?> updateWorker(String workerId, String name, String id, String phoneNumber, String department,String email) {
//        System.out.println(name);
//        System.out.println(department);
        Worker w = findByWorkerId(workerId);
        if (w != null) {
            workerService.updateWorker(workerId, name, id, phoneNumber, department, email);
            return Result.success();
        } else {
            return Result.error("此工号不存在");
        }
    }

    @PutMapping("/deleteWorker")
    public Result<?> deleteWorker(String workerId) {
        Worker w = findByWorkerId(workerId);
        if (w != null) {
            workerService.deleteWorker(workerId);
            return Result.success();
        } else {
            return Result.error("此工号不存在");
        }
    }

    @PutMapping("/login")
    public Result<?> loginWorker(String workerId, String password) {
        Worker w = findByWorkerId(workerId);
        if (w != null) {
            String encryptPsd = Encrypt.toMD5(password);
            if (w.getPassword().equals(encryptPsd)) {
                // 登录成功
                Map<String, Object> claims = new HashMap<>();
                claims.put("userId", workerId);
                claims.put("name", w.getName());
                claims.put("phoneNumber", w.getPhoneNumber());
                claims.put("id", w.getId());
                claims.put("email", w.getEmail());
                claims.put("department", w.getDepartment());
                String token = JwtUtil.genToken(claims);
                return Result.success(token);
            } else {
                return Result.error("密码错误");
            }
        } else {
            return Result.error("不存在的账号");
        }
    }

    @GetMapping("/findRepairman")
    public Result<?> findRepairman() {
        Worker[] repairManList = workerService.findRepairman();
        return Result.success(repairManList);
    }


}
